package org.dreamfly.core.action;

import org.dreamfly.core.bean.ResultSetPool;
import org.dreamfly.core.bean.ServerIpInfo;
import org.dreamfly.core.service.PictureService;
import org.dreamfly.core.service.RadioService;
import org.dreamfly.core.util.ChangeChinese;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-10-11
 * Time: 14:55:56
 * To change this template use File | Settings | File Templates.
 */
public class FileDownloadAction extends BaseAction {
    private String Url; //
    private PictureService pictureService;
    private RadioService radioService;
    private ResultSetPool resultSetPool;
    private String serverName;
    private String applicationName;
    /**
     * 下载缩略图
     *
     * @return
     */
    public String downloadMinPic() {

        return null != UploadFile.fileDownload(response, Url) ? SUCCESS : ERROR;
    }

    public String remoteDownload() {
        ServerIpInfo ipInfo= resultSetPool.getIpInfo(serverName);
        return null != UploadFile.remoteFileDownload(response, Url, ipInfo) ? SUCCESS : ERROR;
    }

    public String getUrl(String server,String picUrl){
        ServerIpInfo ipInfo = resultSetPool.getIpInfo(server);
        String url = "/"+applicationName+"/upload/"+ picUrl;
        URL temp = null;
        try {
            temp = new URL("http", ipInfo.getIp(), ipInfo.getPort(), url);
            return  ChangeChinese.changeURL(temp).toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    public String getURL(){
        ServerIpInfo ipInfo = resultSetPool.getIpInfo(serverName);
        URL temp;
        try {
            temp = new URL("http", ipInfo.getIp(), ipInfo.getPort(), Url);
            response.getWriter().print(ChangeChinese.changeURL(temp).toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String downloadPic() {
        /*try {
            Url = new String(Url.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }*/
        return null != UploadFile.fileDownload(response, Url) ? SUCCESS : ERROR;
    }

    /**
     * 下载音频
     *
     * @return
     */
    public String downloadAudio() {

        return null != UploadFile.fileDownload(response, Url) ? SUCCESS : ERROR;
    }

    public String getUrl() {
        return Url;
    }

    public void setUrl(String url) {
        Url = url;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public void setPictureService(PictureService pictureService) {
        this.pictureService = pictureService;
    }

    public void setRadioService(RadioService radioService) {
        this.radioService = radioService;
    }

    public void setResultSetPool(ResultSetPool resultSetPool) {
        this.resultSetPool = resultSetPool;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }
}
